fix: correct underflow in random_updated_at#4363
fix: correct underflow in random_updated_at#4363metalurgical wants to merge 11 commits intocowprotocol:mainfrom
random_updated_at#4363Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the random_updated_at function to use floating-point multiplication for age calculation and introduces checked_sub to prevent underflow panics when the calculated age exceeds the current time. A unit test was also added to verify this behavior. Feedback was provided regarding the fallback logic, which still contains an unchecked subtraction that could potentially panic if the system uptime is less than one second.
4c187fa to
b66e558
Compare
jmg-duarte
left a comment
There was a problem hiding this comment.
after the saturaring_* changes, please test the edges of the rng (50% and 90%) explicitly too
Good points, thanks. |
Fix underflow when subtracting large durations from Instant in random_updated_at. - Compute age as a percentage of max_age - Use saturating_mul to avoid overflow when calculating age - Clamp Instant subtraction underflow to now via checked_sub(...).unwrap_or(now) - Ensure percentage does not exceed 100 - Adds coverage tests
b66e558 to
c47d8dc
Compare
- Switch to `StdRng::seed_from_u64` for deterministic tests - Remove `assert` on percentage <= 100 - Remove `rand_chacha` dependency.
jmg-duarte
left a comment
There was a problem hiding this comment.
Last note, everything else looks ok
If percentage > 100, clamp 100
…metalurgical/services into fix/random-updated-at-underflow
Description
Fix underflow when subtracting large
DurationsfromInstantinrandom_updated_at.Changes
ageas a percentage ofmax_agesaturating_multo avoid overflow when calculating agechecked_sub(...).unwrap_or(now)How to test
cargo nextest run -p price-estimation